//控制层 
app.controller('inexController', function($scope, $controller, $location, inexService,userService) {

	$controller('baseController', {
		$scope: $scope
	}); //继承
	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		inexService.findAll().success(
			function(response) {
				$scope.list = response;	
			}
		);
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	
	$scope.search = function(page, rows) {
		inexService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				console.info(response);
				$scope.list = response.rows;
				for(var i = 0; i < $scope.list.length; i++) {
					$scope.list[i].createTime = angular.fromJson($scope.list[i].createTime);
				}
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
		
		$scope.getuserInfo();
	}
	$scope.search03 = function(page, rows) {
		inexService.search03(page, rows).success(
			function(response) {
				console.info(response);
				$scope.list = response.rows;
				for(var i = 0; i < $scope.list.length; i++) {
					$scope.list[i].createTime = angular.fromJson($scope.list[i].createTime);
					
				}
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
		
		$scope.getuserInfo();
	}



	//分页
	$scope.findPage = function(page, rows) {
		inexService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//单击隐藏按钮 触发 js 函数事件 将 JayUI 富文本编辑器的内容获取到 并转移到 另一个富文本编辑器中 
		angular.element('#aCountEdit').click();
		//将转换后的文本域内容赋值给 对象属性
		$scope.entity.content = document.getElementById("aCountEdit").value;

		//提交课程前先保存图片到数据库,并拿到相对路径
		if($scope.entity.image != null) {
			inexService.upload().then(function(response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.cover = response.data.data.src;
				$scope.save();
			});
		} else {
			$scope.save();
		}
	}

	//查询实体 
	$scope.findOne = function(id) {
		inexService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);
	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  				
		if($scope.entity.id != null) { //如果有ID
			$scope.entity.createTime = null;
			serviceObject = inexService.update($scope.entity); //修改  
		} else {
			/* 如果分类id为null 附默认值 */
			if($scope.entity.cateId == null){
				$scope.entity.cateId = 0;
			}
			serviceObject = inexService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					//重新查询 
					//$scope.reloadList();//重新加载
					alert(response.message);
					window.location = "inex-list.html";
				} else {
					alert(response.message);
				}
			}
		);
	}

	//批量删除 
	$scope.dele = function() {
		if($scope.selectIds.length < 1) {
			alert("请选中要删除的数据!");
		} else {
			if(confirm('确实要删除 '+$scope.selectIds.length+' 些数据吗?')) {
				//获取选中的复选框			
				inexService.dele($scope.selectIds).success(
					function(response) {
						if(response.success) {
							alert(response.message);
							$scope.reloadList(); //刷新列表
							$scope.selectIds = [];
						}else{
							alert(response.message);
						}
					}
				);
			}
		}
	}

	//单个删除
	$scope.delsFlag = false;
	$scope.delsingle = function(id) {
		if(confirm('确实要删除吗?')) {
			inexService.dele(id).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList();
						$scope.delsFlag = true;
						$timeout(function() {
							$scope.delsFlag = false;
						}, 2000);
					}else{
						alert(response.message);
					}
				}
			);
		}

	}

	//选中全部
	$scope.selectAll = function($event) {
		if($event.target.checked) { //如果是被选中,则增加到数组
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				$scope.selectIds.push(each.id); //将空数组中push新的数据，符合条件的数据的id值
			});
		} else {
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				var idx = $scope.selectIds.indexOf(each.id);
				$scope.selectIds.splice(idx, 1); //删除 
			});
		}
	}

	//编辑页面携带参数
	$scope.edit = function() {
		var id = $location.absUrl().split("?id=")[1];
		$scope.findOne(id);
		$scope.getuserInfo();
	}

	//文章上下线状态
	$scope.updateStatus = function(aId, status) {
		inexService.findOne(aId).success(
			function(response) {
				$scope.entity = response;
				$scope.entity.aState = status;
				$scope.save();
			}
		);

	}
	//对应的数字分类转换成文章描述
	$scope.getCateName = function(cateId) {
		if(cateId=="0"){
			return "收入";
		}
		if(cateId=="1"){
			return "支出";
		}


	}
	//getuserInfo
	//得到对应的用户信息
	$scope.getuserInfo = function() {
		//userService.findByOpenId(openId).success(
		userService.findAll().success(
				function(response){
					console.log("the nickName is"+response.nickName);
					$scope.categoryList = response;
					console.log(response);
					
					
					
				}
		)
		

	}
	//search02
	//searchEntity.gender,searchEntity.nickName
	//搜索
	$scope.search02 = function($event,cateId,openId,time) {
		//$.post("../inex/searchByDetail.do",{"openId":openId,"cateId":cateId,"time":time},function(date){ 
		$.post("../inex/searchByDetail.do",{"openId":openId,"cateId":cateId,"time":time},function(date){ 
		console.info(date);
		console.info(date);
			$scope.entity = date;
			$scope.reloadList()
		})
		
	}
});